package com.fwlst.module_home.setting

import android.content.Context
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.View
import android.view.WindowManager
import com.alibaba.android.arouter.launcher.ARouter
import com.fwlst.lib_base.constant.AppConfig
import com.fwlst.lib_base.constant.SettingRoute
import com.fwlst.lib_base.fragment.BaseMvvmFragment
import com.fwlst.lib_base.model.BaseViewModel
import com.fwlst.module_home.R
import com.fwlst.module_home.databinding.FragmentSoutuSettingBinding


import com.fwlst.module_home.dialog.BaseCommonDialog
import com.fwlst.module_home.dialog.BaseLoadProgressDialog
import com.hjq.toast.Toaster
import java.util.Timer
import java.util.TimerTask

class MySettingFragment : BaseMvvmFragment<FragmentSoutuSettingBinding, BaseViewModel>() {


    override fun initBR(): Int {
        return 0
    }

    override fun initLayout(): Int {
        return R.layout.fragment_soutu_setting
    }

    override fun initView(view: View, savedInstanceState: Bundle?) {

        binding.ivTab1.setOnClickListener {
            goToSetting(SettingRoute.PRIVACY_PARENT)
        }

        binding.ivTab2.setOnClickListener {
            goToSetting(SettingRoute.COMPLAINT_PARENT)
        }

        binding.ivTab3.setOnClickListener {
            goToSetting(SettingRoute.ABOUT_US)

        }
        binding.ivTab4.setOnClickListener {
            ARouter.getInstance().build("/base/route/WebViewActivity")
                .withString("url", AppConfig.USER_SERVICE_URL)
                .navigation()
        }

        binding.ivTab5.setOnClickListener {
            clearCache()
        }
    }

    private fun clearCache() {
        val dialog = BaseCommonDialog(requireContext())
        val loadDialog = BaseLoadProgressDialog(requireContext(), "请等待...")
        dialog.setTitle("清除缓存")
        dialog.setMessage("你确定要清除缓存吗？")
            .setOnClickBottomListener(object : BaseCommonDialog.OnClickBottomListener {
                override fun onPositiveClick() {
                    dialog.dismiss()
                    loadDialog.show()
                    val task: TimerTask = object : TimerTask() {
                        override fun run() {
                            loadDialog.dismiss()
                            Toaster.show("清除成功")
                            //showToastSync("删除成功");
                        }
                    }
                    val timer = Timer()
                    timer.schedule(task, 2000)
                }

                override fun onNegtiveClick() {
                    dialog.dismiss()
                }
            }).show()
        dialog.window!!.setLayout(
            getScreenWidth(requireContext()) - 200,
            WindowManager.LayoutParams.WRAP_CONTENT
        ) //设置弹出框宽度为屏幕宽度的三分之二
    }

    fun goToSetting(pageName: String) {
        ARouter.getInstance().build("/base/route/SettingActivity")
            .withString("pageName", pageName)
            .navigation()
    }

    fun getScreenWidth(context: Context): Int {
        val wm = context.getSystemService("window") as WindowManager
        val dm = DisplayMetrics()
        wm.defaultDisplay.getMetrics(dm)
        return dm.widthPixels
    }
}